﻿namespace FFF.Test.Util.Consts
{
    using System.Collections.Generic;
    using System.Reflection;
    using Microsoft.Extensions.Configuration;
    using Senparc.CO2NET;
    using Senparc.Weixin.Entities;
    using Ydm.Extensions;
    using Ydm.Helper;

    /// <summary>
    /// 定义常量
    /// </summary>
    public static class AppConfigConsts
    {
        /// <summary>
        /// 语言文件的前缀
        /// </summary>
        public static string LocalizationSourceName { get; private set; } = "YdmCodeArtCore";

        /// <summary>
        /// 对应数据连接的名称
        /// </summary>
        public static string ConnectionStringName { get; private set; } = "Master";

        /// <summary>
        /// 从库连接字符串
        /// </summary>
        public static List<string> SlaveConStrs { get; private set; }

        /// <summary>
        /// 记录日志的数据库
        /// </summary>
        public static string LogConStr { get; private set; }

        /// <summary>
        /// 对应的连接字符串在entityModule里面进行初始化
        /// </summary>
        public static string ConStr { get; private set; }

        ///// <summary>
        ///// 用于整个程序读取APPSetting的值
        ///// </summary>
        //public static IConfigurationRoot AppConfigurationRoot { get; set; }

        /// <summary>
        /// redis的连接字符串
        /// </summary>
        public static string RedisConnectionString { get; private set; }

        /// <summary>
        /// redis的数据库的ID
        /// </summary>
        public static int RedisDatabaseId { get; private set; }

        /// <summary>
        /// ElasticSearchUrl的URL
        /// </summary>
        public static string ElasticSearchUrl { get; private set; }

        /// <summary>
        /// 用户的sessionid
        /// </summary>
        public static string UserSessionId { get; private set; } = "YdmSessionId";

        /// <summary>
        /// 用户session
        /// </summary> 
        public static string UserInfo { get; private set; } = "UserInfo";

        /// <summary>
        /// 微信用户Session
        /// </summary> 
        public static string WxUserInfo { get; private set; } = "WxUserInfo";

        /// <summary>
        /// 获取token的用户名称
        /// </summary>
        public static string TokenUserName { get; private set; }

        /// <summary>
        /// 获取token的密码
        /// </summary>
        public static string TokenPassWord { get; private set; }

        /// <summary>
        /// 商业用户名
        /// </summary>
        public static string BuUserName { get; private set; }

        /// <summary>
        /// 商业用户密码
        /// </summary>
        public static string BuUserPassword { get; private set; }

        /// <summary>
        /// 网站的验证码session存储的对象
        /// </summary>
        public static string ValidateCodeStr { get; private set; } = "GenVerificationCode";

        /// <summary>
        /// api的地址
        /// </summary>
        public static string AppDomain { get; set; }

        /// <summary>
        /// 短信的验证码session存储的对象
        /// </summary>
        public static string SsmsCodeStr { get; private set; } = "SsmsCode";

        /// <summary>
        /// 跨域允许的URL
        /// </summary>
        /// <value>The cors origins.</value>
        /// <remarks>于大明</remarks>
        public static string CorsOrigins { get; private set; }

        /// <summary>
        /// 访问数据的api接口
        /// </summary>
        /// <value>The cors origins.</value>
        /// <remarks>于大明</remarks>
        public static string ApiDomain { get; private set; }

        /// <summary>
        /// 盛派微信注册
        /// </summary>
        public static SenparcWeixinSetting SenparcWeixinSetting { get; private set; }

        /// <summary>
        /// 微信全局配置
        /// </summary>
        public static SenparcSetting SenparcSetting { get; private set; }

        /// <summary>
        /// 邮箱的配置
        /// </summary>
        public static EmailSetting EmailSetting { get; private set; }

        /// <summary>
        /// 短信配置
        /// </summary>
        public static SmmsSetting SmmsSetting { get; private set; }

        /// <summary>
        /// 上传图片地址
        /// </summary>
        /// <value>The pic domain.</value>
        /// <remarks>ydm</remarks>
        public static string PicDomain { get; private set; } = "/api/services/app/File/UploadFile";

        /// <summary>
        /// 上传图片地址
        /// </summary>
        /// <value>The pic domain.</value>
        /// <remarks>ydm</remarks>
        public static string PicLookDomain { get; private set; } = string.Empty;

        /// <summary>
        /// 缓存GetPageAsync方法
        /// </summary>
        /// <value>The get page asynchronous method information.</value>
        /// <remarks>于大明</remarks>
        public static MethodInfo GetPageAsyncMethodInfo { get; set; }

        /// <summary>
        /// 阿里云de key
        /// </summary>
        /// <value>The ali access key identifier.</value>
        /// <remarks>ydm</remarks>
        public static string AliAccessKeyId { get; set; } = "LTAI4FciCdFSC8m2UdDh65TQ";

        /// <summary>
        /// 阿里云de key
        /// </summary>
        /// <value>The ali access key identifier.</value>
        /// <remarks>ydm</remarks>
        public static string AliAccessKeySecret { get; set; } = "8Ke0enaWsGI9oG2jqk5AWnGHRpTeBx";

        /// <summary>
        /// 阿里云de key
        /// </summary>
        /// <value>The ali access key identifier.</value>
        /// <remarks>ydm</remarks>
        public static string AliEndpoint { get; set; } = "hangzhou";

        /// <summary>
        /// 阿里云de key
        /// </summary>
        /// <value>The ali access key identifier.</value>
        /// <remarks>ydm</remarks>
        public static string AliBucketName { get; set; } = "yudaminghz";

        /// <summary>
        /// 文件的类型限制
        /// </summary>
        public static Dictionary<string, string> FileTypes
        { get; private set; } = new Dictionary<string, string>
        {
            { ".txt", "text/plain" },
            { ".pdf", "application/pdf" },
            {  ".doc", "application/vnd.ms-word" },
            { ".docx", "application/vnd.ms-word" },
            { ".xls", "application/vnd.ms-excel" },
            { ".xlsx", "application/vnd.openxmlformats   officedocument.spreadsheetml.sheet" },
            { ".png", "image/png" },
            { ".jpg", "image/jpeg" },
            { ".jpeg", "image/jpeg" },
            { ".gif", "image/gif" },
            { ".csv", "text/csv" },
            { ".amr", "audio/amr" },
            { ".wav", "audio/wav" },
        };

        /// <summary>
        /// JUi的op和系统的op做映射
        /// </summary>
        public static Dictionary<string, string> JuiOps { get; private set; } = new Dictionary<string, string>
        {
            { "contains", "like" },  // 包含
            { "equal", "eq" }, // 等于
            { "notequal", "ne" }, // 不等于
            { "greater", "gt" }, // 大于
            { "greaterorequal", "ge" }, // 大于或等于
            { "less", "lt" }, // 小于
            { "lessorequal", "le" }, // 小于或等于
            { "beginwith", "llike" }, // 以...开头
            { "endwith", "rlike" } // 以...结尾
        };

        /// <summary>
        /// 配置JUi的高级查询映射关系
        /// </summary>
        public static Dictionary<string, string> JuiAdvanceOps { get; private set; } = new Dictionary<string, string>
        {
            { "contains", ".Contains({0})" },  // 包含
            { "equal", "={0}" }, // 等于
            { "notequal", "!={0}" }, // 不等于
            { "greater", ">{0}" }, // 大于
            { "greaterorequal", ">={0}" }, // 大于或等于
            { "less", "<{0}" }, // 小于
            { "lessorequal", "<={0}" }, // 小于或等于
            { "beginwith", ".StartsWith({0})" }, // 以...开头
            { "endwith", ".EndsWith({0})" } // 以...结尾
        };

        /// <summary>
        /// 初始化项目配置通过app的json文件
        /// </summary>
        /// <param name="configuration">读取配置文件类</param>
        public static void InitConfigBySettingJson(IConfigurationRoot configuration)
        {
            SenparcWeixinSetting = new SenparcWeixinSetting();
            configuration.GetSection("SenparcWeixinSetting").Bind(SenparcWeixinSetting);

            SenparcSetting = new SenparcSetting();
            configuration.GetSection("SenparcSetting").Bind(SenparcSetting);

            EmailSetting = new EmailSetting();
            configuration.GetSection("App:Eamil").Bind(EmailSetting);

            SmmsSetting = new SmmsSetting();
            configuration.GetSection("App:SSMS").Bind(SmmsSetting);

            // 解密
            var encryptStr = configuration.GetConnectionString(ConnectionStringName);
            ConStr = YdmEncrypt.DecryptString(encryptStr);
            var logStr = configuration.GetConnectionString("LogConStr");
            LogConStr = YdmEncrypt.DecryptString(logStr);

            var salveLst = new List<string>();
            configuration.GetSection("SlaveConStr").Bind(salveLst);
            SlaveConStrs = new List<string>();
            salveLst.ForEach(item => SlaveConStrs.Add(YdmEncrypt.DecryptString(item)));

            RedisConnectionString = configuration["App:Redis:ConnectionString"];
            RedisDatabaseId = configuration["App:Redis:DatabaseId"].ToDefault(0);
            ElasticSearchUrl = configuration["ElasticSearch:Url"];
            TokenUserName = configuration["Authentication:Token:UserName"];
            TokenPassWord = configuration["Authentication:Token:PassWord"];
            BuUserName = configuration["Authentication:BuUser:UserName"];
            BuUserPassword = configuration["Authentication:BuUser:PassWord"];
            CorsOrigins = configuration["App:CorsOrigins"];
            CorsOrigins = configuration["App:ApiDomain"];
        }
    }

    /// <summary>
    /// 邮箱的配置
    /// </summary>
    public class EmailSetting
    {
        /// <summary>
        /// 用户姓名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 用户密码
        /// </summary>
        public string UserPwd { get; set; }

        /// <summary>
        /// email端口
        /// </summary>
        public string EmailPort { get; set; }

        /// <summary>
        /// host
        /// </summary>
        public string SmtpHost { get; set; }

        /// <summary>
        /// email展示的名称
        /// </summary>
        public string DisplayName { get; set; }
    }

    /// <summary>
    /// 阿里云发送短信配置
    /// </summary>
    public class SmmsSetting
    {
        /// <summary>
        /// 阿里云 keyID
        /// </summary>
        public string AccessKeyId { get; set; }

        /// <summary>
        /// 阿里云Secrect
        /// </summary>
        public string AccessKeySecret { get; set; }

        /// <summary>
        /// 阿里云signName
        /// </summary>
        public string SignName { get; set; }
    }
}